package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.BalanceCheckDetailsDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-11-28
 */
public interface BalanceCheckDetailsMapper extends BaseMapper<BalanceCheckDetailsDO> {

    /**
     * 回执付
     *
     * @param id
     * @return
     */
    @Select("SELECT " +
            "bcd.Check_DetailID AS checkDetailid, " +
            "bcd.Check_DetailOwnerID AS checkDetailownerid, " +
            "NULL AS checkDetailorderid, " +
            "bcd.Check_DetailPayType AS checkDetailpaytype, " +
            "bcd.Check_DetailBillCost AS checkDetailbillcost, " +
            "bcd.Check_DetailComments AS checkDetailcomments, " +
            "bcd.Check_DetailBillTime AS checkDetailbilltime, " +
            "bcd.Check_DetailSerType AS checkDetailsertype, " +
            "bcd.Check_DetailBankNo AS checkDetailbankno, " +
            "bcd.Check_DetailSerNo AS checkDetailserno, " +
            "bcd.Check_DetailBillOperator AS checkDetailbilloperator, " +
            "bcd.Check_DetailCreator AS checkDetailcreator, " +
            "bcd.Check_DetailCompanyID AS checkDetailcompanyid, " +
            "'' AS pactCode," +
            "null AS srcOrderId," +
            "bbi.Index_SupplierID AS supplierId, " +
            "bbi.Index_SupplierSymbolID AS supplierSymbolId, " +
            "bbi.Index_DriverID AS driverId, " +
            "bbi.Index_ID AS recId, " +
            "bbi.Index_Bcode AS no, " +
            "bbi.Index_Bname AS bcName, " +
            "bbi.Index_CustomerID AS customerId, " +
            "bbi.Index_OwnerCompanyID AS createCompanyId, " +
            "bbi.Index_Status AS billStatus " +
            "FROM " +
            "Balance_CheckDetails bcd " +
            "LEFT JOIN Balance_BillIndex bbi ON bcd.Check_DetailOwnerID = bbi.Index_ID " +
            "where bcd.Check_DetailID = #{id}")
    BalanceCheckDetailsDO getReturnPayById(Long id);

    /**
     * 预付，到付，货到付款
     *
     * @param id
     * @return
     */
    @Select("SELECT " +
            "bcd.Check_DetailID AS checkDetailid, " +
            "NULL AS checkDetailownerid, " +
            "bcd.Check_DetailOrderID AS checkDetailorderid, " +
            "bcd.Check_DetailPayType AS checkDetailpaytype, " +
            "bcd.Check_DetailBillCost AS checkDetailbillcost, " +
            "bcd.Check_DetailComments AS checkDetailcomments, " +
            "bcd.Check_DetailBillTime AS checkDetailbilltime, " +
            "bcd.Check_DetailSerType AS checkDetailsertype, " +
            "bcd.Check_DetailBankNo AS checkDetailbankno, " +
            "bcd.Check_DetailSerNo AS checkDetailserno, " +
            "bcd.Check_DetailBillOperator AS checkDetailbilloperator, " +
            "bcd.Check_DetailCreator AS checkDetailcreator, " +
            "bcd.Check_DetailCompanyID AS checkDetailcompanyid, " +
            "null AS recId, " +
            "toi.Index_Code AS no, " +
            "'' AS bcName, " +
            "toi.Index_PactCode AS pactCode, " +
            "toi.Index_SrcOrderID AS srcOrderId, " +
            "toi.Index_SupplierID AS supplierId, " +
            "toi.Index_SupplierSymbolID AS supplierSymbolId, " +
            "toi.Index_DriverID AS driverId, " +
            "toi.Index_CustomerID AS customerId, " +
            "toi.Index_CreatorCompanyID AS createCompanyId, " +
            "case b.Index_OwnerCombinedOrderID when 0 then null else b.Index_OwnerCombinedOrderID end AS ownerCombinedOrderId, " +
            "b.Cache_FNAStatus AS financeStatus " +
            "FROM Balance_CheckDetails bcd " +
            "LEFT JOIN TMS_OrderIndex toi ON bcd.Check_DetailOrderID = toi.Index_ID " +
            "LEFT JOIN TMS_OrderIndexCaches b on b.Caches_OrderID = bcd.Check_DetailOrderID " +
            "where bcd.Check_DetailID = #{id}")
    BalanceCheckDetailsDO getNoReturnPayById(Long id);

    /**
     * 列表
     *
     * @return
     */
    @Select("select a.* from Balance_CheckDetails a " +
            "inner JOIN Balance_BillIndex bbi ON a.Check_DetailOwnerID = bbi.Index_ID " +
            "where a.Check_DetailSerType =4 AND (a.Check_DetailOrderID = a.Check_DetailOwnerID OR a.Check_DetailOrderID = 0) and bbi.Index_Status <> 16 " +
            "and a.Check_DetailPayType is not NULL and a.Check_DetailCompanyID > 0 " +
            "order by a.Check_DetailID")
    @DataScope(column = "Check_DetailCompanyID", alias = "a")
    List<BalanceCheckDetailsDO> getReturnPayList();

    @Select("select a.* from Balance_CheckDetails a " +
            "inner JOIN Balance_BillIndex bbi ON a.Check_DetailOwnerID = bbi.Index_ID " +
            "where a.Check_DetailSerType =4 AND (a.Check_DetailOrderID = a.Check_DetailOwnerID OR a.Check_DetailOrderID = 0) and bbi.Index_Status <> 16 " +
            "and a.Check_DetailPayType is not NULL and a.Check_DetailCompanyID > 0 and a.Check_DetailBillTime >= #{startTime} and a.Check_DetailBillTime <= #{endTime} " +
            "order by a.Check_DetailID")
    @DataScope(column = "Check_DetailCompanyID", alias = "a")
    List<BalanceCheckDetailsDO> getReturnPayListTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

    /**
     * 列表
     *
     * @return
     */
    @Select("select a.* from Balance_CheckDetails a " +
            "inner JOIN TMS_OrderIndex toi ON a.Check_DetailOrderID = toi.Index_ID " +
            "where a.Check_DetailSerType IN (1,2,3) and a.Check_DetailPayType is not NULL and a.Check_DetailCompanyID > 0 " +
            "order by a.Check_DetailID ")
    List<BalanceCheckDetailsDO> getNoReturnList();

    @Select("select a.* from Balance_CheckDetails a " +
            "inner JOIN TMS_OrderIndex toi ON a.Check_DetailOrderID = toi.Index_ID " +
            "where a.Check_DetailSerType IN (1,2,3) and a.Check_DetailPayType is not NULL and a.Check_DetailCompanyID > 0 and a.Check_DetailBillTime >= #{startTime} and a.Check_DetailBillTime <= #{endTime} " +
            "order by a.Check_DetailID ")
    List<BalanceCheckDetailsDO> getNoReturnListTime(@Param("startTime") String startTime, @Param("endTime") String endTime);


    /**
     * 根据对账单id查询回执付收付款信息
     *
     * @param billId
     * @param type
     * @return
     */
    @Select("select top 1 * from Balance_CheckDetails bcd where bcd.Check_DetailOwnerID = #{billId} and Check_DetailSerType = #{type} and (Check_DetailOrderID = #{billId} or Check_DetailOrderID = 0) " +
            "order by Check_DetailBillTime  DESC")
    BalanceCheckDetailsDO getReturnPayByBillId(Long billId, Integer type);
}
